<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="options1const vm &#x3D; new Vue(option)  options 的五类属性 数据：data、props、propsData、computed、methods、watch DOM：el、template、render、renderError 生命周期钩子 ：beforeCreate、created、before Mount、mounted、beforeUpdate、updat">
<meta property="og:type" content="article">
<meta property="og:title" content="vue构造选项">
<meta property="og:url" content="http://example.com/2021/10/18/31/index.html">
<meta property="og:site_name" content="Xiaolong&#39;s Blog">
<meta property="og:description" content="options1const vm &#x3D; new Vue(option)  options 的五类属性 数据：data、props、propsData、computed、methods、watch DOM：el、template、render、renderError 生命周期钩子 ：beforeCreate、created、before Mount、mounted、beforeUpdate、updat">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-10-18T04:29:31.000Z">
<meta property="article:modified_time" content="2021-10-22T13:52:13.797Z">
<meta property="article:author" content="Xiaolong">
<meta property="article:tag" content="vue">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/10/18/31/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"http://example.com/2021/10/18/31/","path":"2021/10/18/31/","title":"vue构造选项"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>vue构造选项 | Xiaolong's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Xiaolong's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#options"><span class="nav-number">1.</span> <span class="nav-text">options</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%A5%E9%97%A8%E5%B1%9E%E6%80%A7"><span class="nav-number">2.</span> <span class="nav-text">入门属性</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#components-%E4%B8%89%E7%A7%8D%E5%BC%95%E5%85%A5%E6%96%B9%E5%BC%8F"><span class="nav-number">2.1.</span> <span class="nav-text">components 三种引入方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%9B%E4%B8%AA%E9%92%A9%E5%AD%90"><span class="nav-number">2.2.</span> <span class="nav-text">四个钩子</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#props"><span class="nav-number">2.3.</span> <span class="nav-text">props</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BF%9B%E9%98%B6%E5%B1%9E%E6%80%A7"><span class="nav-number">3.</span> <span class="nav-text">进阶属性</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#computed-%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7"><span class="nav-number">3.1.</span> <span class="nav-text">computed - 计算属性</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BE%8B1-getter-setter-demo"><span class="nav-number">3.1.1.</span> <span class="nav-text">例1. getter &#x2F; setter -demo</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BE%8B2-%E4%BD%BF%E7%94%A8-computed-demo"><span class="nav-number">3.1.2.</span> <span class="nav-text">例2 使用 computed - demo</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BE%8B2-1%E4%B8%8D%E4%BD%BF%E7%94%A8-computed-demo"><span class="nav-number">3.1.3.</span> <span class="nav-text">例2.1不使用 computed - demo</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#watch-%E4%BE%A6%E5%90%AC%E5%99%A8"><span class="nav-number">3.2.</span> <span class="nav-text">watch - 侦听器</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%80%89%E9%A1%B9%EF%BC%9Adeep"><span class="nav-number">3.2.1.</span> <span class="nav-text">选项：deep</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%80%89%E9%A1%B9%EF%BC%9Aimmediate"><span class="nav-number">3.2.2.</span> <span class="nav-text">选项：immediate</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#vm-watch"><span class="nav-number">3.2.3.</span> <span class="nav-text">vm.$watch</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BE%8B1-%E6%92%A4%E9%94%80-demo"><span class="nav-number">3.2.4.</span> <span class="nav-text">例1. 撤销-demo</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BE%8B2-%E6%A8%A1%E6%8B%9F-computed-demo"><span class="nav-number">3.2.5.</span> <span class="nav-text">例2. 模拟 computed-demo</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#directives-%E6%8C%87%E4%BB%A4"><span class="nav-number">3.3.</span> <span class="nav-text">directives - 指令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mixin-%E6%B7%B7%E5%85%A5"><span class="nav-number">3.4.</span> <span class="nav-text">mixin - 混入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#extends-%E7%BB%A7%E6%89%BF"><span class="nav-number">3.5.</span> <span class="nav-text">extends - 继承</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#provide-inject"><span class="nav-number">3.6.</span> <span class="nav-text">provide &#x2F; inject</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Xiaolong"
      src="/images/lion.jpg">
  <p class="site-author-name" itemprop="name">Xiaolong</p>
  <div class="site-description" itemprop="description">Never stop learning</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/drwna" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;drwna" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



          </div>
        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/Drwna" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/10/18/31/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          vue构造选项
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-10-18 12:29:31" itemprop="dateCreated datePublished" datetime="2021-10-18T12:29:31+08:00">2021-10-18</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="options"><a href="#options" class="headerlink" title="options"></a>options</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> vm = <span class="keyword">new</span> <span class="title class_">Vue</span>(option)</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#%E9%80%89%E9%A1%B9-%E6%95%B0%E6%8D%AE">options 的五类属性</a></p>
<p>数据：data、props、propsData、computed、methods、watch</p>
<p>DOM：el、template、render、renderError</p>
<p>生命周期钩子 ：beforeCreate、created、before Mount、mounted、beforeUpdate、updated、activated、deactivated、beforeDestroy、destroyed、errorCaptured</p>
<p>资源：directives、filters、components</p>
<p>组合：parent、mixins、extends、provide、inject</p>
<p>其他：暂且不表</p>
<h2 id="入门属性"><a href="#入门属性" class="headerlink" title="入门属性"></a>入门属性</h2><ul>
<li>el - 挂载点：与 $mount 有替换关系</li>
<li>data - 内部数据：支持对象和函数，优先用函数</li>
<li>methods - 方法： 事件处理函数或者是普通函数</li>
<li>components :  使用 Vue 组件，注意大小写</li>
<li>四个钩子<ul>
<li>created - 实力出现在内存中</li>
<li>mounted - 实力出现在页面中</li>
<li>updated - 示例更新了</li>
<li>destroyed - 实例消亡了</li>
</ul>
</li>
<li>props - 外部属性</li>
</ul>
<h3 id="components-三种引入方式"><a href="#components-三种引入方式" class="headerlink" title="components 三种引入方式"></a>components 三种引入方式</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1 优先使用</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Demo</span> <span class="keyword">from</span> <span class="string">&quot;./Demo.vue&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">	<span class="attr">components</span>:&#123;</span><br><span class="line">        <span class="title class_">Frank1</span>: <span class="title class_">Demo</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">template</span>:<span class="string">`</span></span><br><span class="line"><span class="string">		&lt;div&gt;</span></span><br><span class="line"><span class="string">			...</span></span><br><span class="line"><span class="string">			&lt;Frank/&gt;</span></span><br><span class="line"><span class="string">			...	</span></span><br><span class="line"><span class="string">		&lt;/div&gt;</span></span><br><span class="line"><span class="string">	`</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 2</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="title function_">component</span>(<span class="string">&#x27;Frank2&#x27;</span>,&#123;</span><br><span class="line">    <span class="attr">template</span>:<span class="string">`</span></span><br><span class="line"><span class="string">        &lt;div&gt;demo022&lt;/div&gt;</span></span><br><span class="line"><span class="string">    `</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">template</span>:<span class="string">`</span></span><br><span class="line"><span class="string">		&lt;div&gt;</span></span><br><span class="line"><span class="string">			...</span></span><br><span class="line"><span class="string">			&lt;Frank2/&gt;</span></span><br><span class="line"><span class="string">			...	</span></span><br><span class="line"><span class="string">		&lt;/div&gt;</span></span><br><span class="line"><span class="string">	`</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 3</span></span><br><span class="line"><span class="keyword">const</span> x = &#123;</span><br><span class="line">    <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string">        &lt;div&gt;demo333&lt;/div&gt;</span></span><br><span class="line"><span class="string">    `</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">	<span class="attr">components</span>:&#123;</span><br><span class="line">        <span class="title class_">Frank3</span>: x</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">template</span>:<span class="string">`</span></span><br><span class="line"><span class="string">		&lt;div&gt;</span></span><br><span class="line"><span class="string">			...</span></span><br><span class="line"><span class="string">			&lt;Frank3/&gt;</span></span><br><span class="line"><span class="string">			...	</span></span><br><span class="line"><span class="string">		&lt;/div&gt;</span></span><br><span class="line"><span class="string">	`</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<h3 id="四个钩子"><a href="#四个钩子" class="headerlink" title="四个钩子"></a>四个钩子</h3><p><a target="_blank" rel="noopener" href="https://github.com/Drwna/vue-demo-1/commit/15f8c17930d82109031b7989cc1ea3b3331f55b8#diff-96123f97c22061907b1e0fb96cd06f9bea16682eb3df0bfbfa3192036e0a08c9R18">demo</a></p>
<p>created - 实例出现在内存中</p>
<p>mounted - 实例出现在页面中</p>
<p>updated - 实例更新了</p>
<p>destroyed - 实例从页面和内存中消亡了</p>
<h3 id="props"><a href="#props" class="headerlink" title="props"></a><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#props">props</a></h3><p>props 可以是数组或对象，用于接收来自父组件的数据。props 可以是简单的数组，或者使用对象作为替代，对象允许配置高级选项，如类型检测、自定义验证和设置默认值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">message=<span class="string">&quot;n&quot;</span>  <span class="comment">// 传入字符串</span></span><br><span class="line">:message=<span class="string">&quot;n&quot;</span> <span class="comment">// 传入 this.n</span></span><br><span class="line">:fn=<span class="string">&quot;add&quot;</span> <span class="comment">// 传入 this.add 函数</span></span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// Demo.vue</span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div&gt;</span><br><span class="line">    ”这是 demo 的内部“,</span><br><span class="line">    n = &#123;&#123; message &#125;&#125;</span><br><span class="line">    &lt;button @click=&quot;fn&quot;&gt;call back&lt;/button&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  props: [&#x27;message&#x27;, &#x27;fn&#x27;]</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// main.js</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Demo</span> <span class="keyword">from</span> <span class="string">&#x27;./Demo.vue&#x27;</span></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">    <span class="attr">components</span>: &#123;<span class="title class_">Demo</span>&#125;,</span><br><span class="line">    <span class="title function_">data</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;<span class="attr">n</span>: <span class="number">5</span>&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string">      &lt;div&gt;</span></span><br><span class="line"><span class="string">      &#123;&#123; n &#125;&#125;</span></span><br><span class="line"><span class="string">      &lt;Demo :message=&quot;n&quot; :fn=&quot;add&quot;/&gt;</span></span><br><span class="line"><span class="string">      &lt;/div&gt;</span></span><br><span class="line"><span class="string">    `</span>,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">        <span class="title function_">add</span>(<span class="params"></span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">n</span> += <span class="number">1</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).$mount(<span class="string">&#x27;#app2&#x27;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="进阶属性"><a href="#进阶属性" class="headerlink" title="进阶属性"></a>进阶属性</h2><h3 id="computed-计算属性"><a href="#computed-计算属性" class="headerlink" title="computed - 计算属性"></a>computed - <a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#computed">计算属性</a></h3><p>被计算出来的属性就是<strong>计算属性</strong>。</p>
<ul>
<li>当其依赖的属性的值发生变化的时，计算属性会重新计算。</li>
<li>如果依赖的属性没有变化，就不会重新计算，使用<strong>缓存</strong>中的属性值。</li>
</ul>
<p>computed 比较适合对多个变量或者对象进行处理后返回一个结果值，也就是数多个变量中的某一个值发生了变化则我们监控的这个值也就会发生变化。</p>
<h4 id="例1-getter-setter-demo"><a href="#例1-getter-setter-demo" class="headerlink" title="例1. getter / setter -demo"></a>例1. getter / setter -demo</h4><p><a target="_blank" rel="noopener" href="https://codesandbox.io/s/compassionate-lake-xyjkw">用户名展示</a></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 需引用完整版 vue</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="property">config</span>.<span class="property">productionTip</span> = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">  <span class="attr">data</span>: &#123;</span><br><span class="line">    <span class="attr">user</span>: &#123;</span><br><span class="line">      <span class="attr">email</span>: <span class="string">&quot;fangyinghang@qq.com&quot;</span>,</span><br><span class="line">      <span class="attr">nickname</span>: <span class="string">&quot;方方&quot;</span>,</span><br><span class="line">      <span class="attr">phone</span>: <span class="string">&quot;13812312312&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">computed</span>: &#123;</span><br><span class="line">    <span class="attr">displayName</span>: &#123;</span><br><span class="line">      <span class="title function_">get</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> user = <span class="variable language_">this</span>.<span class="property">user</span>;</span><br><span class="line">        <span class="keyword">return</span> user.<span class="property">nickname</span> || user.<span class="property">email</span> || user.<span class="property">phone</span>;</span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="title function_">set</span>(<span class="params">value</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(value);</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">user</span>.<span class="property">nickname</span> = value;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string">    &lt;div&gt;</span></span><br><span class="line"><span class="string">      &#123;&#123;displayName&#125;&#125;</span></span><br><span class="line"><span class="string">      &lt;div&gt;</span></span><br><span class="line"><span class="string">      &#123;&#123;displayName&#125;&#125;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;add&quot;&gt;set&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;/div&gt;</span></span><br><span class="line"><span class="string">    &lt;/div&gt;</span></span><br><span class="line"><span class="string">  `</span>,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    <span class="title function_">add</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;add&quot;</span>);</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">displayName</span> = <span class="string">&quot;圆圆&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;).$mount(<span class="string">&quot;#app&quot;</span>);</span><br></pre></td></tr></table></figure>



<h4 id="例2-使用-computed-demo"><a href="#例2-使用-computed-demo" class="headerlink" title="例2 使用 computed - demo"></a>例2 使用 computed - demo</h4><p><a target="_blank" rel="noopener" href="https://codesandbox.io/s/little-breeze-hdx44">用 computed 筛选男女</a></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Vue</span>.<span class="property">config</span>.<span class="property">productionTip</span> = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> id = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">createUser</span> = (<span class="params">name, gender</span>) =&gt; &#123;</span><br><span class="line">  id += <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">return</span> &#123; <span class="attr">id</span>: id, <span class="attr">name</span>: name, <span class="attr">gender</span>: gender &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">  <span class="title function_">data</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">users</span>: [</span><br><span class="line">        <span class="title function_">createUser</span>(<span class="string">&quot;方方&quot;</span>, <span class="string">&quot;男&quot;</span>),</span><br><span class="line">        <span class="title function_">createUser</span>(<span class="string">&quot;圆圆&quot;</span>, <span class="string">&quot;女&quot;</span>),</span><br><span class="line">        <span class="title function_">createUser</span>(<span class="string">&quot;小新&quot;</span>, <span class="string">&quot;男&quot;</span>),</span><br><span class="line">        <span class="title function_">createUser</span>(<span class="string">&quot;小葵&quot;</span>, <span class="string">&quot;女&quot;</span>)</span><br><span class="line">      ],</span><br><span class="line">      <span class="attr">displayUsers</span>: []</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="title function_">created</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">displayUsers</span> = <span class="variable language_">this</span>.<span class="property">users</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    <span class="title function_">showMale</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">displayUsers</span> = <span class="variable language_">this</span>.<span class="property">users</span>.<span class="title function_">filter</span>(<span class="function"><span class="params">u</span> =&gt;</span> u.<span class="property">gender</span> === <span class="string">&quot;男&quot;</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">showFemale</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">displayUsers</span> = <span class="variable language_">this</span>.<span class="property">users</span>.<span class="title function_">filter</span>(<span class="function"><span class="params">u</span> =&gt;</span> u.<span class="property">gender</span> === <span class="string">&quot;女&quot;</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">showAll</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">displayUsers</span> = <span class="variable language_">this</span>.<span class="property">users</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string">    &lt;div&gt;</span></span><br><span class="line"><span class="string">      &lt;div&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;showAll&quot;&gt;全部&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;showMale&quot;&gt;男&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;showFemale&quot;&gt;女&lt;/button&gt;&lt;/div&gt;</span></span><br><span class="line"><span class="string">      &lt;ul&gt;</span></span><br><span class="line"><span class="string">        &lt;li v-for=&quot;(u,index) in displayUsers&quot; :key=&quot;index&quot;&gt;&#123;&#123;u.name&#125;&#125; - &#123;&#123;u.gender&#125;&#125;&lt;/li&gt;</span></span><br><span class="line"><span class="string">      &lt;/ul&gt;</span></span><br><span class="line"><span class="string">      </span></span><br><span class="line"><span class="string">    &lt;/div&gt;</span></span><br><span class="line"><span class="string">  `</span></span><br><span class="line">&#125;).$mount(<span class="string">&quot;#app&quot;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h4 id="例2-1不使用-computed-demo"><a href="#例2-1不使用-computed-demo" class="headerlink" title="例2.1不使用 computed - demo"></a>例2.1不使用 computed - demo</h4><p><a target="_blank" rel="noopener" href="https://codesandbox.io/s/modest-matsumoto-pd03w">不用 computed 筛选男女</a></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Vue</span>.<span class="property">config</span>.<span class="property">productionTip</span> = <span class="literal">false</span>;</span><br><span class="line"><span class="keyword">let</span> id = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">createUser</span> = (<span class="params">name, gender</span>) =&gt; &#123;</span><br><span class="line">  id += <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">id</span>: id,</span><br><span class="line">    <span class="attr">name</span>: name,</span><br><span class="line">    <span class="attr">gender</span>: gender,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">  <span class="title function_">data</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">users</span>: [</span><br><span class="line">        <span class="title function_">createUser</span>(<span class="string">&quot;方方&quot;</span>, <span class="string">&quot;男&quot;</span>),</span><br><span class="line">        <span class="title function_">createUser</span>(<span class="string">&quot;圆圆&quot;</span>, <span class="string">&quot;女&quot;</span>),</span><br><span class="line">        <span class="title function_">createUser</span>(<span class="string">&quot;小新&quot;</span>, <span class="string">&quot;男&quot;</span>),</span><br><span class="line">        <span class="title function_">createUser</span>(<span class="string">&quot;小葵&quot;</span>, <span class="string">&quot;女&quot;</span>),</span><br><span class="line">      ],</span><br><span class="line">      <span class="attr">gender</span>: <span class="string">&quot;&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">computed</span>: &#123;</span><br><span class="line">    <span class="title function_">displayUsers</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="keyword">const</span> hash = &#123;</span><br><span class="line">        <span class="attr">male</span>: <span class="string">&quot;男&quot;</span>,</span><br><span class="line">        <span class="attr">female</span>: <span class="string">&quot;女&quot;</span>,</span><br><span class="line">      &#125;;</span><br><span class="line">      <span class="keyword">const</span> &#123; users, gender &#125; = <span class="variable language_">this</span>;</span><br><span class="line">      <span class="keyword">if</span> (gender === <span class="string">&quot;&quot;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> users;</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> gender === <span class="string">&quot;string&quot;</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> users.<span class="title function_">filter</span>(<span class="function">(<span class="params">u</span>) =&gt;</span> u.<span class="property">gender</span> === hash[gender]);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">Error</span>(<span class="string">&quot;gender 的值是意外的值&quot;</span>);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    <span class="title function_">setGender</span>(<span class="params">string</span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">gender</span> = string;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string">    &lt;div&gt;</span></span><br><span class="line"><span class="string">      &lt;div&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;setGender(&#x27;&#x27;) &quot;&gt;全部&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;setGender(&#x27;male&#x27;)&quot;&gt;男&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;setGender(&#x27;female&#x27;)&quot;&gt;女&lt;/button&gt;&lt;/div&gt;</span></span><br><span class="line"><span class="string">      &lt;ul&gt;</span></span><br><span class="line"><span class="string">        &lt;li v-for=&quot;(u,index) in displayUsers&quot; :key=&quot;index&quot;&gt;&#123;&#123;u.name&#125;&#125; - &#123;&#123;u.gender&#125;&#125;&lt;/li&gt;</span></span><br><span class="line"><span class="string">      &lt;/ul&gt;</span></span><br><span class="line"><span class="string">      </span></span><br><span class="line"><span class="string">    &lt;/div&gt;</span></span><br><span class="line"><span class="string">  `</span>,</span><br><span class="line">&#125;).$mount(<span class="string">&quot;#app&quot;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>







<p><a target="_blank" rel="noopener" href="https://v3.cn.vuejs.org/guide/computed.html#%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E5%92%8C%E4%BE%A6%E5%90%AC%E5%99%A8">官方 计算属性和侦听器</a></p>
<h3 id="watch-侦听器"><a href="#watch-侦听器" class="headerlink" title="watch - 侦听器"></a>watch - <a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#watch">侦听器</a></h3><ul>
<li>使用watch来响应数据的变化，也就是当数据变化时，执行一个函数。</li>
<li>一般用于异步或者开销较大的操作</li>
<li>watch 中的属性 一定是 data 中 已经存在的数据 </li>
<li><strong>当需要监听一个对象的改变时，普通的 watch 方法无法监听到对象内部属性的改变，只有 data 中的数据才能够监听到变化，此时就需要 deep 属性对对象进行深度监听</strong></li>
</ul>
<h4 id="选项：deep"><a href="#选项：deep" class="headerlink" title="选项：deep"></a>选项：deep</h4><p>为了发现对象内部值的变化，可以在选项参数中指定 <code>deep: true</code>。注意监听数组的变更不需要这么做。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">vm.$watch(<span class="string">&#x27;someObject&#x27;</span>, callback, &#123;</span><br><span class="line">  <span class="attr">deep</span>: <span class="literal">true</span></span><br><span class="line">&#125;)</span><br><span class="line">vm.<span class="property">someObject</span>.<span class="property">nestedValue</span> = <span class="number">123</span></span><br><span class="line"><span class="comment">// callback is fired</span></span><br></pre></td></tr></table></figure>

<h4 id="选项：immediate"><a href="#选项：immediate" class="headerlink" title="选项：immediate"></a>选项：immediate</h4><p>在选项参数中指定 <code>immediate: true</code> 将立即以表达式的当前值触发回调：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">vm.$watch(<span class="string">&#x27;a&#x27;</span>, callback, &#123;</span><br><span class="line">  <span class="attr">immediate</span>: <span class="literal">true</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 立即以 `a` 的当前值触发回调</span></span><br></pre></td></tr></table></figure>

<p>注意在带有 <code>immediate</code> 选项时，你不能在第一次回调时取消侦听给定的 property。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这会导致报错</span></span><br><span class="line"><span class="keyword">var</span> unwatch = vm.$watch(</span><br><span class="line">  <span class="string">&#x27;value&#x27;</span>,</span><br><span class="line">  <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="title function_">doSomething</span>()</span><br><span class="line">    <span class="title function_">unwatch</span>()</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123; <span class="attr">immediate</span>: <span class="literal">true</span> &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>如果你仍然希望在回调内部调用一个取消侦听的函数，你应该先检查其函数的可用性：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> unwatch = vm.$watch(</span><br><span class="line">  <span class="string">&#x27;value&#x27;</span>,</span><br><span class="line">  <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="title function_">doSomething</span>()</span><br><span class="line">    <span class="keyword">if</span> (unwatch) &#123;</span><br><span class="line">      <span class="title function_">unwatch</span>()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123; <span class="attr">immediate</span>: <span class="literal">true</span> &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>



<h4 id="vm-watch"><a href="#vm-watch" class="headerlink" title="vm.$watch"></a><a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/api/#vm-watch">vm.$watch</a></h4><h4 id="例1-撤销-demo"><a href="#例1-撤销-demo" class="headerlink" title="例1. 撤销-demo"></a>例1. 撤销-demo</h4><p><a target="_blank" rel="noopener" href="https://codesandbox.io/s/lucid-shamir-cpcw3">https://codesandbox.io/s/lucid-shamir-cpcw3</a></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Vue</span>.<span class="property">config</span>.<span class="property">productionTip</span> = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">  <span class="attr">data</span>: &#123;</span><br><span class="line">    <span class="attr">n</span>: <span class="number">0</span>,</span><br><span class="line">    <span class="attr">history</span>: [],</span><br><span class="line">    <span class="attr">inUndoMode</span>: <span class="literal">false</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">watch</span>: &#123;</span><br><span class="line">    <span class="attr">n</span>: <span class="keyword">function</span> (<span class="params">newValue, oldValue</span>) &#123;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>.<span class="property">inUndoMode</span>);</span><br><span class="line">      <span class="keyword">if</span> (!<span class="variable language_">this</span>.<span class="property">inUndoMode</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">history</span>.<span class="title function_">push</span>(&#123; <span class="attr">from</span>: oldValue, <span class="attr">to</span>: newValue &#125;);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 不如用 computed 来计算 displayName</span></span><br><span class="line">  <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string">    &lt;div&gt;</span></span><br><span class="line"><span class="string">      &#123;&#123;n&#125;&#125;</span></span><br><span class="line"><span class="string">      &lt;hr /&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;add1&quot;&gt;+1&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;add2&quot;&gt;+2&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;minus1&quot;&gt;-1&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;minus2&quot;&gt;-2&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;hr/&gt;</span></span><br><span class="line"><span class="string">      &lt;button @click=&quot;undo&quot;&gt;撤销&lt;/button&gt;</span></span><br><span class="line"><span class="string">      &lt;hr/&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">      &#123;&#123;history&#125;&#125;</span></span><br><span class="line"><span class="string">    &lt;/div&gt;</span></span><br><span class="line"><span class="string">  `</span>,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    <span class="title function_">add1</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">n</span> += <span class="number">1</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">add2</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">n</span> += <span class="number">2</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">minus1</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">n</span> -= <span class="number">1</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">minus2</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">n</span> -= <span class="number">2</span>;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">undo</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="keyword">const</span> last = <span class="variable language_">this</span>.<span class="property">history</span>.<span class="title function_">pop</span>();</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">inUndoMode</span> = <span class="literal">true</span>;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;ha&quot;</span> + <span class="variable language_">this</span>.<span class="property">inUndoMode</span>);</span><br><span class="line">      <span class="keyword">const</span> old = last.<span class="property">from</span>;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">n</span> = old; <span class="comment">// watch n 的函数会异步调用</span></span><br><span class="line">      <span class="variable language_">this</span>.$nextTick(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">inUndoMode</span> = <span class="literal">false</span>;</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;).$mount(<span class="string">&quot;#app&quot;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h4 id="例2-模拟-computed-demo"><a href="#例2-模拟-computed-demo" class="headerlink" title="例2. 模拟 computed-demo"></a>例2. <a target="_blank" rel="noopener" href="https://codesandbox.io/s/objective-star-vu2h3">模拟 computed-demo</a></h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引用完整版 Vue，方便讲解</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Vue</span> <span class="keyword">from</span> <span class="string">&quot;vue/dist/vue.js&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="title class_">Vue</span>.<span class="property">config</span>.<span class="property">productionTip</span> = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Vue</span>(&#123;</span><br><span class="line">  <span class="attr">data</span>: &#123;</span><br><span class="line">    <span class="attr">user</span>: &#123;</span><br><span class="line">      <span class="attr">email</span>: <span class="string">&quot;fangfang@qq.com&quot;</span>,</span><br><span class="line">      <span class="attr">nickname</span>: <span class="string">&quot;方方&quot;</span>,</span><br><span class="line">      <span class="attr">phone</span>: <span class="string">&quot;13812312312&quot;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">displayName</span>: <span class="string">&quot;&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">watch</span>: &#123;</span><br><span class="line">    <span class="string">&quot;user.email&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">handler</span>: <span class="string">&quot;changed&quot;</span>,</span><br><span class="line">      <span class="attr">immediate</span>: <span class="literal">true</span> <span class="comment">// 第一次渲染是也触发 watch</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;user.nickname&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">handler</span>: <span class="string">&quot;changed&quot;</span>,</span><br><span class="line">      <span class="attr">immediate</span>: <span class="literal">true</span> <span class="comment">// 第一次渲染是也触发 watch</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;user.phone&quot;</span>: &#123;</span><br><span class="line">      <span class="attr">handler</span>: <span class="string">&quot;changed&quot;</span>,</span><br><span class="line">      <span class="attr">immediate</span>: <span class="literal">true</span> <span class="comment">// 第一次渲染是也触发 watch</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 不如用 computed 来计算 displayName</span></span><br><span class="line">  <span class="attr">template</span>: <span class="string">`</span></span><br><span class="line"><span class="string">    &lt;div&gt;</span></span><br><span class="line"><span class="string">       &#123;&#123;displayName&#125;&#125;</span></span><br><span class="line"><span class="string">       &lt;button @click=&quot;user.nickname=undefined&quot;&gt;remove nickname&lt;/button&gt;</span></span><br><span class="line"><span class="string">    &lt;/div&gt;</span></span><br><span class="line"><span class="string">  `</span>,</span><br><span class="line">  <span class="attr">methods</span>: &#123;</span><br><span class="line">    <span class="title function_">changed</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">arguments</span>);</span><br><span class="line">      <span class="keyword">const</span> user = <span class="variable language_">this</span>.<span class="property">user</span>;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="property">displayName</span> = user.<span class="property">nickname</span> || user.<span class="property">email</span> || user.<span class="property">phone</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;).$mount(<span class="string">&quot;#app&quot;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h3 id="directives-指令"><a href="#directives-指令" class="headerlink" title="directives - 指令"></a>directives - 指令</h3><ul>
<li>内置指令 v-if / v-for / v-bind / v-on</li>
<li>自定义指令</li>
<li>指令是为了减少重复的 dom 操作</li>
</ul>
<h3 id="mixin-混入"><a href="#mixin-混入" class="headerlink" title="mixin - 混入"></a>mixin - 混入</h3><ul>
<li>重复三次之后的出路</li>
<li>混入 V.S. 全局混入</li>
<li>选项自动合并</li>
<li>混入就是为了减少重复的构造选项</li>
</ul>
<p>还是使用上一个例子：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title class_">MyVue</span> = <span class="title class_">Vue</span>.<span class="title function_">extend</span>(&#123;</span><br><span class="line">    <span class="title function_">data</span>(<span class="params"></span>)&#123; <span class="keyword">return</span> &#123;<span class="attr">name</span>:<span class="string">&#x27;&#x27;</span>, <span class="attr">time</span>: <span class="literal">undefined</span>&#125; &#125;,</span><br><span class="line">    <span class="title function_">created</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="keyword">if</span>(!<span class="variable language_">this</span>.<span class="property">name</span>)&#123; <span class="variable language_">console</span>.<span class="title function_">erroe</span>(<span class="string">&#x27;no name!&#x27;</span>) &#125;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="property">time</span> = <span class="keyword">new</span> <span class="title class_">Date</span>()</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">beforeDestroy</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="keyword">const</span> duration = (<span class="keyword">new</span> <span class="title class_">Date</span>()) - <span class="variable language_">this</span>.<span class="property">time</span></span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">`<span class="subst">$&#123;<span class="variable language_">this</span>.time&#125;</span> 存活时间 <span class="subst">$&#123;duration&#125;</span>`</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>









<h3 id="extends-继承"><a href="#extends-继承" class="headerlink" title="extends - 继承"></a>extends - 继承</h3><ul>
<li>先了解一下 Vue.extend</li>
<li>你觉得用了 mixin 还是重复</li>
<li>于是你自己写了一个 View, 他继承 Vue</li>
<li>你还可以预先定义其他构造选项</li>
<li>继承就是为了减少重复的构造选项</li>
<li>那为什么不用 ES6 的extends 呢？</li>
</ul>
<h3 id="provide-inject"><a href="#provide-inject" class="headerlink" title="provide / inject"></a>provide / inject</h3><ul>
<li>爷爷想和孙子讲话怎么办</li>
<li>祖宗想跟他的所有后代讲话怎么办</li>
<li>答案是全局变量，但是全局变量太 low</li>
<li>所以我们需要 局部的全局变量</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/vue/" rel="tag"><i class="fa fa-tag"></i> vue</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/16/56/" rel="prev" title="vue数据响应式的理解">
                  <i class="fa fa-chevron-left"></i> vue数据响应式的理解
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/20/01/" rel="next" title="Vue模板">
                  Vue模板 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Xiaolong</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  





</body>
</html>
